﻿/********************************************************************************************
 * 文件名称:	ListItem.cs
 * 设计人员:	yan(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	管理员
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;
using Shanfree.SharpMovie.DALFactory;

namespace Shanfree.SharpMovie.BLL
{
    /// <summary>
    /// 管理员
    /// </summary>
    public class ListItem
    {
        private static readonly IListItem dal = DataAccess.CreateItem();

        /// <summary>
        /// 根据列表获取电影项
        /// </summary>
        /// <param name="movieID">用户主键</param>
        /// <returns></returns>
        public IList<ListItemInfo> GetList(string listID, int pageSize, int pageIndex)
        {
            if (string.IsNullOrEmpty(listID))
            {
                throw new ArgumentNullException("listID");
            }
            return dal.GetList(listID, pageSize, pageIndex);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="menuID">ID</param>
        /// <returns></returns>
        public ListItemInfo GetByID(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            return dal.GetByID(id);
        }

        /// <summary>
        /// 添加电影项
        /// </summary>
        /// <param name="listID">列表主键</param>
        /// <param name="movieID">电影主键</param>
        /// <param name="remark">说明</param>
        /// <param name="sortIndex">排序号</param>
        /// <returns>电影项主键</returns>
        public string Add(string listID, string movieID, string remark)
        {
            if (string.IsNullOrEmpty(listID))
            {
                throw new ArgumentNullException("listID不能为空。");
            }
            if (string.IsNullOrEmpty(movieID))
            {
                throw new ArgumentNullException("movieID不能为空。");
            }
            if (string.IsNullOrEmpty(remark))
            {
                throw new ArgumentNullException("remark不能为空。");
            }
            ListItemInfo nInfo = new ListItemInfo();
            nInfo.CreateTime = DateTime.Now;
            nInfo.ListID = listID;
            nInfo.MovieID = movieID;
            nInfo.Remark = remark;
            string listItemID = dal.Add(nInfo);
            nInfo.ID = listItemID;
            // 更新排序号
            int count = dal.Count(listID);
            nInfo.SortIndex = count;
            dal.Update(nInfo);
            return listItemID;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">主键</param>
        public void Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            ListItemInfo nInfo = new ListItemInfo();
            nInfo.ID = id;
            dal.Delete(nInfo);
        }

        /// <summary>
        /// 更新电影项
        /// </summary>
        /// <param name="itemID">列表主键</param>
        /// <param name="movieID">电影主键</param>
        /// <param name="remark">说明</param>
        /// <param name="sortIndex">排序号</param>
        /// <returns>电影项主键</returns>
        public void Update(string itemID,string remark, int sortIndex)
        {
            if (string.IsNullOrEmpty(itemID))
            {
                throw new ArgumentNullException("itemID不能为空。");
            }
            if (sortIndex < 1)
            {
                throw new ArgumentException("sortIndex不能小于1。");
            }
            dal.Update(itemID, remark, sortIndex);
        }
                /// <summary>
        /// 更新电影项
        /// </summary>
        /// <param name="listID">列表主键</param>
        /// <param name="movieID">电影主键</param>
        /// <param name="remark">说明</param>
        /// <param name="sortIndex">排序号</param>
        /// <returns>电影项主键</returns>
        public void Update(string listID, string itemID, string imdb,string remark, int sortIndex)
        {
            if (string.IsNullOrEmpty(listID))
            {
                throw new ArgumentNullException("listID不能为空。");
            }
            if (string.IsNullOrEmpty(itemID))
            {
                throw new ArgumentNullException("itemID不能为空。");
            }
            if (sortIndex < 1)
            {
                throw new ArgumentException("sortIndex不能小于1。");
            }
            ListItemInfo lInfo = dal.GetByID(itemID);
            MovieInfo mInfo = new Movie().GetByIMDB_NO(imdb);
            lInfo.MovieID = mInfo.ID;
            lInfo.ListID = listID;
            lInfo.Remark = remark;
            dal.Update(lInfo);
        }
    }
}
